﻿using System;
using System.Linq;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web.DynamicData;

namespace DD_Extensions
{
    public partial class BooleanRadioButtonsField : System.Web.DynamicData.FieldTemplateUserControl
    {
        public override string FieldValueString
        {
            get
            {
                var trueValue = Boolean.TrueString;
                var falseValue = Boolean.FalseString;
                var filterUIHint = Column.Attributes.OfType<UIHintAttribute>().FirstOrDefault();
                if (filterUIHint != null &&
                    filterUIHint.ControlParameters.Count > 0 &&
                    filterUIHint.ControlParameters.ContainsKey("TrueValue") &&
                    filterUIHint.ControlParameters.ContainsKey("FalseValue"))
                {
                    trueValue = filterUIHint.ControlParameters["TrueValue"].ToString();
                    falseValue = filterUIHint.ControlParameters["FalseValue"].ToString();
                }

                if ((Boolean)FieldValue)
                    return trueValue;
                else
                    return falseValue;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // add CSS from meta data
            this.ApplyCss();
        }

        public override Control DataControl
        {
            get { return Literal1; }
        }
    }
}
